package com.xiaohu.mayi_huiyuan.service;

import com.xiaohu.mayi_huiyuan.common.Mode;
import com.xiaohu.mayi_huiyuan.entity.*;
import com.xiaohu.mayi_huiyuan.mapper.CustomerMapper;
import com.xiaohu.mayi_huiyuan.mapper.PasswordupdatetableMapper;
import com.xiaohu.mayi_huiyuan.mapper.YuebiaoMapper;
import com.xiaohu.mayi_huiyuan.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 注册  消费
 */
@Service
public class RegisterAndConsumption {

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    YuebiaoMapper yuebiaoMapper;


    @Autowired
    PasswordupdatetableMapper passwordupdatetableMapper;

    /**
     * 用户注册
     *
     * @param customer
     */
    public void insertCustomer(Customer customer, HttpSession session) {
        customer.setCreatedate(new Date());
        Usrs usrs = (Usrs) session.getAttribute("user");
        customer.setCreateid(usrs.getId());
        customer.setPassword(MD5.md5Password(customer.getPassword()));
        customer.setIdcard(MD5.md5Password(customer.getIdcard()));
        customerMapper.insert(customer);
    }

    /**
     * 验证手机号码  是否被注册
     */
    public boolean vaPhone(String phone) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andPhoneEqualTo(phone);
        List<Customer> list = customerMapper.selectByExample(customerExample);
        if (list.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查看手机号码  或者 卡号 是否存在
     *
     * @param phoneAndKahao
     * @return
     */
    public String isExist(String phoneAndKahao) {
        if (!StringUtils.isEmpty(customerMapper.vaphoneandkaohao(phoneAndKahao))) {
            return "yes";
        }
        return "no";
    }

    /**
     * 用户充值
     */
    public void insertChongZhi(String phoneAndKahao, String miaoshu, String jine, HttpSession session) {
        Yuebiao yuebiao = new Yuebiao();
        if (jine.indexOf(".") != -1) {
            jine = jine.substring(0, jine.indexOf("."));
        }
        yuebiao.setChongzhie(new BigDecimal(jine));
        Integer customerId = getCustomerId(phoneAndKahao);
        yuebiao.setCustomerid(customerId);
        Usrs user = (Usrs) session.getAttribute("user");
        yuebiao.setCreateid(user.getId());
        yuebiao.setCreatedate(new Date());
        Double chongzhiyue = getCustomerChongZhiYue(customerId);
        if (chongzhiyue == null) {
            yuebiao.setChongzhiyue(new BigDecimal(jine));
        } else {
            yuebiao.setChongzhiyue(new BigDecimal(jine).add(new BigDecimal(chongzhiyue)));
        }
        yuebiao.setMode(Integer.parseInt(Mode.CHONGZHI));
        yuebiao.setMiaoshu(miaoshu);
        yuebiaoMapper.insert(yuebiao);
    }


    /**
     * 获取用户的ID
     *
     * @param phoneAndKahao
     * @return
     */
    public Integer getCustomerId(String phoneAndKahao) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria1 = customerExample.createCriteria();
        criteria1.andPhoneEqualTo(phoneAndKahao);
        CustomerExample.Criteria criteria2 = customerExample.createCriteria();
        criteria2.andKahaoEqualTo(phoneAndKahao);
        customerExample.or(criteria2);
        List<Customer> list = customerMapper.selectByExample(customerExample);
        if (list.size() > 0) {
            return list.get(0).getId();
        }
        return null;
    }

    /**
     * 查询 用户 充值 的 余额
     *
     * @param customerId
     * @return
     */

    public Double getCustomerChongZhiYue(Integer customerId) {
        return yuebiaoMapper.selctMaxChongzhiYue(customerId);
    }

    /**
     * @param phoneOrKahao
     * @param jine
     * @param miaoshu
     * @param session
     */
    @Transactional(rollbackFor = Exception.class)
    public void xiaoFei_FanXian(String phoneOrKahao, Double jine, String miaoshu, HttpSession session) {
        Yuebiao yuebiao = new Yuebiao();
        Integer customerId = getCustomerId(phoneOrKahao);
        Integer createId = ((Usrs) session.getAttribute("user")).getId();
        Double fanxianlv = Double.parseDouble(Mode.FANXIANLV);
        yuebiao.setCreateid(createId);
        yuebiao.setMode(Integer.parseInt(Mode.FANGXIAN_XIAOFEI));
        yuebiao.setMiaoshu(miaoshu);
        yuebiao.setCreatedate(new Date());
        yuebiao.setCustomerid(customerId);
        yuebiao.setFanxainlv(new BigDecimal(fanxianlv));
        yuebiao.setFanxiane(new BigDecimal(jine).multiply(new BigDecimal(fanxianlv)));
        yuebiao.setXiaofeie(new BigDecimal(jine));
        Double old_fanxianyue = getFanXianYue(customerId);
        if (old_fanxianyue == null) {
            yuebiao.setChongjian(new BigDecimal(0.00));
        } else {
            yuebiao.setChongjian(new BigDecimal(old_fanxianyue));
        }
        yuebiao.setFanxianyue(new BigDecimal(jine).multiply(new BigDecimal(fanxianlv)));
        yuebiaoMapper.insertSelective(yuebiao);
    }

    /**
     * 获取返现 余额
     *
     * @return
     */
    public Double getFanXianYue(Integer customerId) {
        return yuebiaoMapper.selctMaxFanXianYue(customerId);
    }

    /**
     * 通过 phone or kahao 查询返现余额
     *
     * @return
     */
    public Double selectFanXianYue(String phoneOrKahao) {
        Integer customerId = getCustomerId(phoneOrKahao);
        return getFanXianYue(customerId);
    }

    /**
     * 通过 phone or kahao 查询充值余额
     *
     * @return
     */
    public Double selectChongZhiYue(String phoneOrKahao) {
        Integer customerId = getCustomerId(phoneOrKahao);
        return getCustomerChongZhiYue(customerId);
    }


    /**
     * 充值消费
     *
     * @param phoneorkahao
     * @param jine
     * @param miaoshu
     * @param password
     * @param session
     * @return
     */
    public String insertChongZhiXiaoFei(String phoneorkahao, String jine, String miaoshu, String password, HttpSession session) {
        jine = jine.trim();
        Integer customerId = getCustomerId(phoneorkahao);
        Customer customer = customerMapper.selectByPrimaryKey(customerId);
        if (!StringUtils.isEmpty(customer) && !StringUtils.isEmpty(password)) {
            if (!MD5.md5Password(password).equals(customer.getPassword())) {
                return "passerror";
            }
        }
        Yuebiao yuebiao = new Yuebiao();
        Integer createId = ((Usrs) session.getAttribute("user")).getId();
        yuebiao.setCreateid(createId);
        yuebiao.setCreatedate(new Date());
        yuebiao.setCustomerid(customerId);
        yuebiao.setMiaoshu(miaoshu);
        Double chongZhiYue = getCustomerChongZhiYue(customerId);
        if (chongZhiYue == null) {
            return "yuebuzu";
        }
        if (new BigDecimal(chongZhiYue).compareTo(new BigDecimal("0")) == 0 || new BigDecimal(chongZhiYue).compareTo(new BigDecimal("0")) == -1) {
            return "yuebuzu";
        }
        yuebiao.setXiaofeie(new BigDecimal(jine));
        if (new BigDecimal(chongZhiYue).compareTo(new BigDecimal(jine)) == -1) {
            yuebiao.setChongzhiyue(new BigDecimal("0"));
            yuebiao.setMode(Integer.parseInt(Mode.CHONGZHI_FANXIAN_XIAOFEI));
            yuebiao.setChongjian(new BigDecimal(chongZhiYue));
            BigDecimal chae = new BigDecimal(jine).subtract(new BigDecimal(chongZhiYue));
            BigDecimal fanxiane = chae.multiply(new BigDecimal(Mode.FANXIANLV));
            yuebiao.setFanxiane(fanxiane);
            Double fanxianyue = getFanXianYue(customerId);
            if (fanxianyue != null) {
                yuebiao.setFanxianyue(new BigDecimal(fanxianyue).add(fanxiane));
            }
            yuebiaoMapper.insertSelective(yuebiao);
        } else {
            yuebiao.setChongzhiyue(new BigDecimal(chongZhiYue).subtract(new BigDecimal(jine)));
            yuebiao.setMode(Integer.parseInt(Mode.CHONGZHIYUEE_XIAOFEI));
            yuebiao.setChongjian(new BigDecimal(jine));
            yuebiaoMapper.insertSelective(yuebiao);
        }
        return "ok";
    }

    /**
     * 修改密码
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String changePassword(String phoneOrKahao, String shenfenzheng, String xinmima, HttpSession session) {
        Customer customer = customerMapper.getCustomer(phoneOrKahao);
        if (StringUtils.isEmpty(customer)) {
            throw new NullPointerException("changePassword:customer is null");
        }
        if (!StringUtils.isEmpty(shenfenzheng)) {
            if (!MD5.md5Password(shenfenzheng).equals(customer.getIdcard())) {
                return "shenerror";
            }
        }
        String oldPassword = customer.getPassword();
        customer.setPassword(MD5.md5Password(xinmima));
        customerMapper.updatePassword(customer);
        Passwordupdatetable passwordupdatetable = new Passwordupdatetable();
        passwordupdatetable.setCreatedate(new Date());
        passwordupdatetable.setCustomerid(customer.getId());
        passwordupdatetable.setNewpass(MD5.md5Password(xinmima));
        Integer id = ((Usrs) session.getAttribute("user")).getId();
        passwordupdatetable.setOldpass(oldPassword);
        passwordupdatetable.setCreateid(id);
        passwordupdatetableMapper.insert(passwordupdatetable);
        return "ok";
    }


}
